﻿/*
 * Create By:Jeff Yuan
 * Create Time : 2009-03-10 10:54
 * 本控件使用方法，宿主必须是GRIDVIEW，WEB环境可用。
 * 0. AllowPaging =true，Visible=false
 * 1. 在使用必须先引用GIRDVIEW到 HOSTGIRDVIEW 
 * 2. 在GIRDVIEW数据绑定后 调用FillPager()填充
 * 3. 在本控件的OnPagerChanging事件的方法中 添加GIRDVIEW的数据绑定操作
 * 
 * Edit By: Arthur.li
 * Edit At: 2009年4月23日 11:15:11
 * 添加点击几个翻页按钮时的判断条件, 解决在GridView未绑定数据时,点击按钮的显示问题
 * 
 */
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.Persistent;
public partial class UserControl_GridViewEX : System.Web.UI.UserControl
{
    #region 事件
    public delegate void PagerChangingHandler();
    public event PagerChangingHandler PagerChanging;
    public virtual void OnPagerChanging()
    {
        if (PagerChanging != null)
        {
            PagerChanging();
        }
    }
    #endregion 


    #region 属性
    private GridView _hostgridview = null;
    private string HostGridViewID
    {
        get { return ViewState[this.UniqueID] == null ? "" : ViewState[this.UniqueID].ToString(); }
        set { ViewState[this.UniqueID] = value; }
    }
    public GridView HostGridView
    {
        get { return _hostgridview; }
        set { _hostgridview = value; HostGridViewID = _hostgridview.UniqueID; }
    }
    #endregion


    protected void Page_Load(object sender, EventArgs e)
    {
    }
    /// <summary>
    /// 从WEB上提取GIRDVIEW
    /// </summary>
    private void GetHostGVFromPage()
    {
        if (ViewState[this.UniqueID] != null)
        {
            HostGridView = (GridView)((Page)Context.Handler).FindControl(ViewState[UniqueID].ToString());
        }
    }
    /// <summary>
    /// 如果绑定了GRIDVIEW则自动填充内容！
    /// </summary>
    public void FillPager()
    {
        GetHostGVFromPage();
        FillPage();
    }
    private void FillPage()
    {
        try
        {
            //判断数据源，是否有数据并且填充“总记录数”
            if ( this._hostgridview.PageCount == 1||this._hostgridview.DataSource == null)//GRIGVIEW没有 无需分页
            {
                this.Visible = false;
                return;
            }
            else
            {
                this.Visible = true;
            }
            if (this._hostgridview.DataSource.GetType() == typeof(DataTable))
            {
                this.lblCount.Text = ((DataTable)this._hostgridview.DataSource).Rows.Count.ToString();
            }
            else if (this._hostgridview.DataSource.GetType() == typeof(ObjectTable))
            {
                this.lblCount.Text = ((ObjectTable)this._hostgridview.DataSource).Count.ToString();
            }
            else
            {
                throw new Exception("页控件无法识别数据源类型");
            }
            //页数
            this.lblPageCount.Text = this._hostgridview.PageCount.ToString();
            //当前页
            this.lblPageCur.Text = (this._hostgridview.PageIndex + 1).ToString();
            if (lblPageCur.Text != "1")
            {
                this.btnFirst.Enabled = true;
                this.btnPrev.Enabled = true;
            }
            else
            {
                this.btnFirst.Enabled = false;
                this.btnPrev.Enabled = false;
            }
            if (lblPageCur.Text != lblPageCount.Text)
            {
                this.btnLast.Enabled = true;
                this.btnNext.Enabled = true;
            }
            else
            {
                this.btnLast.Enabled = false;
                this.btnNext.Enabled = false;
            }

        }
        catch
        {
            throw new Exception("页控件错误");
        }

    }

    #region 按钮响应
    protected void btnGo_Click(object sender, EventArgs e)
    {
        if (_hostgridview.PageCount.Equals(0))
        {
            return;
        }

        this.OnPagerChanging();        
        GetHostGVFromPage();
        int newPageIndex = 0;
        if (int.TryParse(txtNewPageIndex.Text, out newPageIndex))
        {
            if (newPageIndex <= 0)
            {
                newPageIndex = 1;
            }

            newPageIndex = newPageIndex - 1;
            newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
            if(newPageIndex >= _hostgridview.PageCount)
            {
                newPageIndex = _hostgridview.PageCount - 1;
                txtNewPageIndex.Text = _hostgridview.PageCount.ToString();
            }
            this._hostgridview.PageIndex = newPageIndex;
        }
        FillPage();
        return;
    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        GetHostGVFromPage();
        if (HostGridView.PageCount > 0 && !HostGridView.PageIndex.Equals(0))
        {
            HostGridView.PageIndex = 0;
            this.OnPagerChanging();
            FillPage();
        }
    }
    protected void btnPrev_Click(object sender, EventArgs e)
    {
       
        GetHostGVFromPage();
        if (HostGridView.PageIndex > 0)
        {
            HostGridView.PageIndex = HostGridView.PageIndex - 1;

            this.PagerChanging();
            FillPage();
        } 
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        
        GetHostGVFromPage();
        if (HostGridView.PageIndex < HostGridView.PageCount - 1)
        {
            HostGridView.PageIndex = HostGridView.PageIndex + 1;

            this.OnPagerChanging();
            FillPage();
        }
    }
    protected void btnLast_Click(object sender, EventArgs e)
    {
     
        GetHostGVFromPage();
        if (HostGridView.PageCount > HostGridView.PageIndex)
        {
            HostGridView.PageIndex = HostGridView.PageCount - 1;
            this.OnPagerChanging();
            FillPage();
        }
    }
    #endregion
}
